
import uiPanel from "./../../lib/UiPanel";
const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingAudio extends uiPanel {
    // @property(cc.Sprite)
    // musicONOff: cc.Sprite = null;
    // @property(cc.Sprite)
    // soundONOff: cc.Sprite = null;
    // @property(cc.SpriteFrame)
    // on_spriteFrame: cc.SpriteFrame = null;
    // @property(cc.SpriteFrame)
    // off_spriteFrame: cc.SpriteFrame = null;
    @property(cc.Label)
    lbl_version: cc.Label = null;

    @property(cc.Slider)
    musicSlider: cc.Slider = null; //滑条;
    @property(cc.ProgressBar)
    musicProgressbar: cc.ProgressBar = null; //进度条;

    @property(cc.Slider)
    soundSlider: cc.Slider = null; //滑条;
    @property(cc.ProgressBar)
    soundProgressbar: cc.ProgressBar = null; //进度条;

    //可更换的sprite
    @property(cc.Sprite)
    bgSpr: cc.Sprite = null;
    @property(cc.Sprite)
    topSpr: cc.Sprite = null;
    @property(cc.Sprite)
    topTitleSpr: cc.Sprite = null;
    @property(cc.Sprite)
    topCloseBtnSpr: cc.Sprite = null;
    @property(cc.Sprite)
    contentSpr: cc.Sprite = null;


    @property(cc.Sprite)
    sp_musicSlider: cc.Sprite = null;
    @property(cc.Sprite)
    musicSpr: cc.Sprite = null;
    @property(cc.Sprite)
    musicProgressSpr: cc.Sprite = null;
    @property(cc.Sprite)
    musicProgressHandleSpr: cc.Sprite = null;
    @property(cc.Sprite)
    sp_effectSlider: cc.Sprite = null;
    @property(cc.Sprite)
    effectSpr: cc.Sprite = null;
    @property(cc.Sprite)
    effectProgressSpr: cc.Sprite = null;
    @property(cc.Sprite)
    effectProgressHandleSpr: cc.Sprite = null;

    musicVolume = 0;
    soundVolume = 0;


    onLoad() {
        // this.setMusicSwitch();
        // this.setSoundSwitch();
        this.lbl_version.string = "版本号：" + cc.vv.GLConfig.getLocalVersion();

        this.setMusicVolume();
        this.setSoundVolume();
    }
    onEnable() {
        // cc.vv.syskeyMgr.putCloseCallBack(this.onClickBack.bind(this), this);
    }
    onDisable() {
        // cc.vv.syskeyMgr.rmvCloseCallBack(this);
    }

    init(args: [{
        atlas: string //, //图集: 图集地址
    }]) {
        let url = args[0] || { atlas: 'Hall/SettingAudio/SettingAudio' };
        //test-start
        let data = {
            atlas: url.atlas,
            bgSpr: 'TK_dt', //大背景 : 在图集里的名字
            topSpr: 'TK_dbl', //top
            topTitleSpr: 'TK_z_sz', //topTitle'设置'
            topCloseBtnSpr: 'TK_an_gb', //关闭按钮
            contentSpr: 'TK_dt2', //content背景
            sp_musicSlider: "TK_sz_hdt_dt",
            musicSpr: 'TK_yy', //'音乐'
            musicProgressSpr: 'TK_sz_hdt', //音乐进度条
            musicProgressHandleSpr: 'TK_hk', //音乐控制器
            sp_effectSlider: "TK_sz_hdt_dt",
            effectSpr: 'TK_yx', //'音效'
            effectProgressSpr: 'TK_sz_hdt', //音效进度条
            effectProgressHandleSpr: 'TK_hk', //音效控制器
        }
        //test-end
        if (data) {
            this.getAtlas(data, this.changeSprite.bind(this));
        }
    }

    //获取传入图集
    private getAtlas(args, callback: Function = null) {
        var atlas = cc.loader.getRes(args['atlas'], cc.SpriteAtlas);
        if (atlas) {
            callback(atlas, args);
        }
        else {
            cc.loader.loadRes(args['atlas'], cc.SpriteAtlas, function (err, atlas) {
                if (err) { }
                else {
                    if (this.node)
                        callback(atlas, args);
                }
            }.bind(this));
        }
    }
    //切换图片
    private changeSprite(atlas: cc.SpriteAtlas, args) {
        for (var i in args) {
            // if (!atlas.getSpriteFrame(args[i])) continue;
            if (!this[i]) continue;
            this[i].spriteFrame = atlas.getSpriteFrame(args[i]);


        }
    }

    setMusicVolume() {
        this.musicVolume = cc.vv.GLSettingDataModel.getMusicVolume();
        if (this.musicVolume < 0) {
            this.musicVolume = 0;
        } else if (this.musicVolume > 1) {
            this.musicVolume = 1;
        }
        this.musicSlider.progress = this.musicVolume;
        this.musicProgressbar.progress = this.musicVolume;
    }
    setSoundVolume() {
        this.soundVolume = cc.vv.GLSettingDataModel.getSoundVolume();
        if (this.soundVolume < 0) {
            this.soundVolume = 0;
        } else if (this.soundVolume > 1) {
            this.soundVolume = 1;
        }
        this.soundSlider.progress = this.soundVolume;
        this.soundProgressbar.progress = this.soundVolume;
    }

    //滑动条事件回调;
    onMusicSliderEvent(slider, noSlider) {
        this.musicVolume = this.musicSlider.progress;
        this.musicProgressbar.progress = this.musicSlider.progress;
        cc.vv.GLSettingDataModel.setMusciVolume(this.musicVolume);
    }

    onSoundSliderEvent(slider, noSlider) {

        this.soundVolume = this.soundSlider.progress;
        this.soundProgressbar.progress = this.soundSlider.progress;
        cc.vv.GLSettingDataModel.setSoundVolume(this.soundVolume);
        console.log("soundVolume = " + this.soundVolume);
    }
    onClickBack() {
        cc.vv.GLSettingDataModel.setMusciVolume(this.musicVolume);
        cc.vv.GLSettingDataModel.setSoundVolume(this.soundVolume);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        cc.vv.UIMgr.close(cc.vv.GLConfig.SettingAudio_path, cc.vv.UIMgr.closeType.destory)
    }
}
